define(['jquery'], function () {
    $('footer').load('../html/footer.html')
    class Shop {
        constructor() {
            var user = getCookie('用户')
            var users = JSON.parse(localStorage.getItem('curt'))
            var jugement = users.findIndex((item) => {
                return item.userName == user
            })
            this.tbody = document.querySelector('tbody')
            this.list = JSON.parse(localStorage.getItem('shop'+jugement)) || []
            this.selectAll = document.querySelector('.selectAll')
            this.allNum = document.querySelector('.allNum')
            this.allCount = document.querySelector('.allCount')
            this.bigBox = document.querySelector('.bigBox')
            this.dlAll = document.querySelector('.dlAll')
            this.calc = document.querySelector('.calc')
            this.num = 0
            this.number = 0
            this.init()
            console.log(111)
        }
        init() {
            this.show()
            this.check()
            // this.getMoney()
            this.deleteAll()
            this.jump()
            this.change()
            this.jude()
        }
        jude() {
            console.log($('.login'))
            if (getCookie('用户')) {
                let cookie = getCookie('用户')
                $('.login').html(`<a href="../html/member.html">${cookie}<img src="../imgaes/index/钻石.png" alt=""></a>`)
            }
        }
        show() {
            // $('footer').load('../html/footer.html')
            this.tbody.innerHTML = ''
            this.tbody.innerHTML = ''
            if (this.list.length > 0) {
                this.list.forEach((item) => {
                    this.tbody.innerHTML += `
                                        
                        <tr>
                        <td><input type="checkbox" value="" class="select" data-id="${item.goods_id}" ${item.is_select ? 'checked' : ''} /></td>
                        <td>
                            <img src="${item.img_small_logo}" >
                            <span class="hide">${item.title}</span>
                        </td>
                        <td><span>￥${item.current_price}</span></td>
                        <td>
                            <span class="sub" data-id="${item.goods_id}">-</span>
                            <input type="text" value="${item.cart_number}" class="txt" data-id="${item.goods_id}"/>
                            <span class="add" data-id="${item.goods_id}">+</span>
                        </td>
                        <td><span class="price">${item.current_price * item.cart_number}元</span></td>
                        <td><span class="del" data-id="${item.goods_id}">删除</span></td>
                        </tr>
                     `
                });

            } else {
                this.tbody.innerHTML = `
                         <tr height="100">
                         <td colspan="6">
                          <h1>快去买点东西吧亲<a href="shop.html">Go</a></h1>
                         </td>
                         </tr>
                                `
            }
            this.change()
        }

        check() {
            var user = getCookie('用户')
            var users = JSON.parse(localStorage.getItem('curt'))||[]
            var jugement = users.findIndex((item) => {
                return item.userName == user
            })
            this.bigBox.addEventListener('click', (e) => {

                var e = e || window.event
                var target = e.target || e.srcElement
                // 全选
                if (target.className == 'selectAll') {
                    this.list.forEach(item => {
                        item.is_select = this.selectAll.checked
                        console.log(item.is_select)
                    })
                    this.show()
                }
                // 单选
                if (target.className == 'select') {
                    let id = target.dataset.id
                    let targetSelcet = this.list.find(item => {
                        return item.goods_id == id
                    })
                    targetSelcet.is_select = target.checked
                    let charge = this.list.every(item => {
                        return item.is_select == true
                    })
                    if (charge) {
                        this.selectAll.checked = true
                    } else {
                        this.selectAll.checked = false
                    }
                    this.show()
                }
                // 增加
                if (target.className == 'add') {
                    let id = target.dataset.id
                    let targetAdd = this.list.find(item => {
                        return item.goods_id == id
                    })
                    targetAdd.cart_number++
                    localStorage.setItem('shop'+jugement, JSON.stringify(this.list))
                    this.show()
                }
                //  减少
                if (target.className == 'sub') {
                    console.log(11)
                    let id = target.dataset.id
                    let targetSub = this.list.find(item => {
                        return item.goods_id == id
                    })
                    if (targetSub.cart_number > 1) {
                        targetSub.cart_number--
                        localStorage.setItem('shop'+jugement, JSON.stringify(this.list))
                    } else {
                        alert('你不能再减少咯!')
                    }
                    this.show()
                }


                // 删除
                if (target.className == 'del') {
                    let id = target.dataset.id
                    let targetDelete = this.list.filter(item => {
                        return item.goods_id != id
                    })
                    this.list = targetDelete
                    localStorage.setItem('shop'+jugement, JSON.stringify(this.list)) || []
                    this.show()
                }
                // 算总价
                this.getMoney()
            })
        }
        getMoney() {
            let targeThing = this.list.filter(item => {
                return item.is_select == true
            })
            this.num = 0
            this.number = 0
            targeThing.forEach(item => {
                this.num += item.current_price * item.cart_number
                this.number += item.cart_number
            })

            this.allNum.innerHTML = `已选商品${this.number}件`
            this.allCount.innerHTML = '总价:' + `<b class="money">￥${this.num}</b>`
        }
        deleteAll() {
            var user = getCookie('用户')
            var users = JSON.parse(localStorage.getItem('curt'))||[]
            var jugement = users.findIndex((item) => {
                return item.userName == user
            })
            this.dlAll.addEventListener('click', () => {
                this.list = []
                localStorage.setItem('shop'+jugement, JSON.stringify(this.list))
                this.show()
            })
        }
        jump() {
            var user = getCookie('用户')
            var users = JSON.parse(localStorage.getItem('curt'))
            var jugement = users.findIndex((item) => {
                return item.userName == user
            })
            this.calc.addEventListener('click', () => {
                // self.location = 'money.html'
                let targeThing = this.list.filter(item => {
                    return item.is_select == true
                })
                console.log(targeThing);
                localStorage.setItem('store'+jugement, JSON.stringify(targeThing))
                self.location = 'money.html'
            })
        }
        change() {

            var inputs = document.querySelectorAll('.txt')

            console.log(inputs)
            inputs.forEach(item => {
                item.addEventListener('blur', () => {
                    console.log(11)
                    let id = item.dataset.id
                    let targetNum = this.list.find((item1) => {
                        return item1.goods_id == id
                    })
                    let inputNum = Number(item.value)
                    console.log(inputNum)
                    if (inputNum > 0) {
                        targetNum.cart_number = inputNum
                    } else {
                        alert('请输入大于0的数字')
                    }
                    this.show()
                })
            })
        }
    }
    new Shop()


})